package cn.zwk.controller;

import cn.zwk.common.model.R;
import cn.zwk.domain.system.model.dto.MenuDTO;
import cn.zwk.domain.system.model.vo.MenuComponentVo;
import cn.zwk.domain.system.model.vo.MenuLabelVo;
import cn.zwk.domain.system.service.IMenuService;
import cn.zwk.repository.po.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {


    @Autowired
    private IMenuService menuService;
    /**
     * 获取路由树
     */
    @GetMapping("/routes")
    public R<List<Menu>> treeRoutes() {
        return R.ok(menuService.treeRoutes());
    }

    @GetMapping("/menus")
    public R<List<Menu>> treeMenus() {
        return R.ok(menuService.treeMenus());
    }

    /**
     * 获取组件列表,用于添加菜单
     * @return
     */
    @GetMapping("/components")
    public R<List<MenuComponentVo>> componentList() {
        return R.ok(menuService.menuComponentList());
    }

    @GetMapping("/{id}")
    public R<MenuDTO> getInfo(@PathVariable("id") Long id){
        return R.ok(menuService.getInfo(id));
    }

    /**
     * 获取菜单选项,用于角色分配菜单
     * @return
     */
    @GetMapping("/options")
    public R listMenuOptions() {
        List<MenuLabelVo> menus = menuService.listMenuOptions();
        return R.ok(menus);
    }

    @PostMapping("/add")
    public R add(@RequestBody MenuDTO menuDto) {
        return menuService.addMenu(menuDto)?R.ok():R.fail();
    }

    /*不能下移到自己的子孩子*/
    @PutMapping("/edit")
    public R edit(@RequestBody MenuDTO menuDto) {
        return menuService.editMenu(menuDto)?R.ok():R.fail();
    }

    @DeleteMapping("/del/{id}")
    public R<Void> del(@PathVariable("id") Long id) {
        menuService.deleteMenu(id);
        return R.ok();
    }

}
